<?php
include_once('../../php/postgre.php');

$act = '';
if(isset($_POST['act'])) $act = $_POST['act'];

if ($act=='simpan_udesa'){
	$idusulankec = $_POST['id_usulankec'];
	//$iddesa = $_POST['iddesa'];
	$skpd = $_POST['skpd'];
	$idkegiatan = $_POST['idkegiatan'];
	$indhslpro = $_POST['ind_hslpro'];
	$indklr = $_POST['ind_klr'];
	$indhslkeg = $_POST['ind_hslkeg'];
	$targethsl = $_POST['target'];
	$tahun = $_POST['tahun'];
	$kegiatan = $_POST['kTambahan'];
	$id_rinc_usulan_kec = $_POST['id']; 
	$lokasi = $_POST['lokasi'];
	$volume = $_POST['volume'];
	$apbdkab = $_POST['apbdkab'];
	$apbdprov = $_POST['apbdprov'];
	$apbn = $_POST['apbn'];
	$prioritas = $_POST['prioritas'];
	$rows  = $_POST['rows'];
	
	if (!is_numeric ($idusulankec)){
		echo "Err: invalid id";
		exit;
	}
	if ($idusulankec>0){
		if($kegiatan==0){
			exec_query("update mus_usulankecamatan set id_skpd=".$skpd.", id_kegiatan=".$idkegiatan.", indikator_hasil='".$indhslkeg."', indikator_program='".$indhslpro."', indikator_keluaran='".$indklr."', target_hasil='".$targethsl."' where id_usulankec=".$idusulankec);
		}else{
			exec_query("update mus_usulankecamatan set id_skpd=".$skpd.",  indikator_hasil='".$indhslkeg."', indikator_program='".$indhslpro."', indikator_keluaran='".$indklr."', target_hasil='".$targethsl."', kegiatan='".$kegiatan."' where id_usulankec=".$idusulankec);
		}
			for ($i=0; $i<count($rows); $i++){
				exec_query("update mus_rinc_usulan_kec set volume=".$volume[$i].", apbdkab=".$apbdkab[$i].", apbdprov=".$apbdprov[$i].", apbn=".$apbn[$i].", prioritas=".$prioritas[$i].", lokasi='".$lokasi[$i]."' where id_rinc_usulan_kec=".$id[$i]);
			}
		echo 'success__';
		
		logs('Mengubah usulan Desa','ID SKPD '.$skpd.'; ID kegiatan '.$idkegiatan);
	}else{
		if($kegiatan == ''){
			$maxid = get_maxid ('id_usulankec', 'mus_usulankecamatan');
			exec_query("insert into mus_usulankecamatan (id_usulankec, id_skpd, id_kegiatan, indikator_hasil, indikator_program, indikator_keluaran, target_hasil, tahun, kegiatan) values(".$maxid.", ".$skpd.", ".$idkegiatan.", '".$indhslkeg."', '".$indhslpro."', '".$indklr."', '".$targethsl."', ".$tahun.", 0)");
		}else{
			$maxid = get_maxid ('id_usulankec', 'mus_usulankecamatan');
			exec_query("insert into mus_usulankecamatan (id_usulankec, id_skpd, id_kegiatan, indikator_hasil, indikator_program, indikator_keluaran, target_hasil, tahun, kegiatan) values(".$maxid.", ".$skpd.", 0, '".$indhslkeg."', '".$indhslpro."', '".$indklr."', '".$targethsl."', ".$tahun.", '".$kegiatan."')");
		}
		$stored = get_data ('select id_usulankec from mus_usulankecamatan where id_usulankec='.$maxid);
		
		//Simpan Rincian
		for ($i=0; $i<count($rows); $i++){
			$maxid1 = get_maxid ('id_rinc_usulan_kec', 'mus_rinc_usulan_kec');
			exec_query("insert into mus_rinc_usulan_kec (id_rinc_usulan_kec, id_usulankec, volume, apbdkab, apbdprov, apbn, prioritas, lokasi) values(".$maxid1.", ".$maxid.", '".$volume[$i]."', ".$apbdkab[$i].", ".$apbdprov[$i].", ".$apbn[$i].", ".$prioritas[$i].", '".$lokasi[$i]."')");
			$stored1 = get_data ('select id_rinc_usulan_kec from mus_rinc_usulan_kec where id_rinc_usulan_kec='.$maxid1);
			if($stored1['id_rinc_usulan_kec']==$maxid1){
				$id_rinc_usulan_kec = $maxid1;
			}

		}
		
		if($stored['id_usulankec']==$maxid){
			echo 'success__';
			$idusulankec = $maxid;
		}
		
		logs('Menambah usulan Desa ','SKPD '.$skpd.'; ID kegiatan '.$idkegiatan.'; tahun '.$tahun);
	}
		
	echo $idusulankec.'__';
	view_udesa();
	
}else if($act=='del_udesa'){
	$idusulankec = $_POST['id_usulankec'];
	//echo $idusulankec ;
	$data = array();
	if(is_numeric($idusulankec)){
		$data = get_data('select * from mus_usulankecamatan where id_usulankec='.$idusulankec);
		exec_query("delete from mus_usulankecamatan where id_usulankec=".$idusulankec);
		exec_query("delete from mus_rinc_usulan_kec where id_usulankec=".$idusulankec);
		echo 'success__';
		view_udesa();
		
	}else{
		echo 'Error ';
	}
	
	logs('Menghapus usulan Desa',implode('; ',$data));

}else if($act=='edit_udesa'){
	$idusulankec = $_POST['id_usulankec'];
	if(is_numeric($idusulankec)){
		$data = get_data("select u.id_usulankec, s.id_skpd, s.nama_skpd, u.indikator_program, u.indikator_keluaran, u.indikator_hasil, u.target_hasil,  b.id_bdgbapeda, u.id_kegiatan, u.kegiatan from mus_subbidangbappeda su, mus_bidang_bappeda b, mus_skpd s, mus_usulankecamatan u where u.id_skpd=s.id_skpd and s.id_subbidangbappeda=su.id_subbidangbappeda and su.id_bdgbapeda=b.id_bdgbapeda and u.id_usulankec=".$idusulankec);
		
			$keg = get_data ("select p.id_program from mus_program p, mus_kegiatan k where k.id_program=p.id_program and k.id_keg=".$data['id_kegiatan']);
			
		
		$sql = get_datas ("select * from mus_skpd where id_skpd=".$data['id_skpd']);
		$idskpd='__';
			foreach ($sql as $sql){
				$idskpd .= '<option value = "'.$sql['id_skpd'].'">'.$sql['nama_skpd'].'</option>';
			}
		$ress = implode($data,'__');
		print_r ($ress);
		echo '__'.$keg['id_program'];
		echo $idskpd;
		$rin= get_datas ("select * from mus_rinc_usulan_kec where id_usulankec=".$data['id_usulankec']);
		echo "__<table align='center'>";
		$counter = 0;
		foreach ($rin as $rin){
			echo "<tr>
				<td><input type='hidden' name='id[]' value=".$rin['id_rinc_usulan_kec']."><input type='text' name='lokasi[]' value=".$rin['lokasi']."></td>
				<td><input type='text' name='volume[]' value=".$rin['volume']."></td>
				<td><input type='text' name='apbdkab[]' value=".$rin['apbdkab']."></td>
				<td><input type='text' name='apbdprov[]' value=".$rin['apbdprov']."></td>
				<td><input type='text' name='apbn[]' value=".$rin['apbn']."><input type='hidden' name='rows[]' value='".$counter++."'></td>
				<td><input type='text' name='prioritas[]' value=".$rin['prioritas']." style='width:50px'></td>

			</tr>";
		}
		echo "</table>";
		
		$prokeg = get_data ("select p.nama_program, k.nama_kegiatan from mus_kegiatan k, mus_program p where p.id_program=k.id_program and k.id_keg=".$data['id_kegiatan']);
		echo "__".$prokeg['nama_program']."|".$prokeg['nama_kegiatan'];
	}else{
		echo 'Error';
	}
}

function view_udesa(){

                $udesa = get_datas("select distinct u.id_usulankec, u.id_kegiatan, s.nama_skpd, u.indikator_hasil, u.indikator_program, u.indikator_keluaran, u.target_hasil, u.tahun, u.kegiatan from mus_usulankecamatan u, mus_kegiatan k, mus_skpd s where s.id_skpd=u.id_skpd and (u.id_kegiatan=k.id_keg or u.id_kegiatan=0) order by u.id_usulankec");
                foreach($udesa as $udesa){
					if ($udesa['id_kegiatan'] == 0 ){
						$nama_kegiatan = $udesa['kegiatan'];
					}else{
						$desa = get_data ("select nama_kegiatan from mus_kegiatan where id_keg=".$udesa['id_kegiatan']);
						$nama_kegiatan = $desa['nama_kegiatan'];
					}
					
                ?>
                <tr>
                    <td style="text-align:center"><?php echo $udesa['id_usulankec']?></td>
                    <td><a href="#showDetail" data-toggle="modal" id="kegiatan" name="kegiatan" onclick="detail(<?php echo $udesa['id_usulankec']?>)"><?php echo $nama_kegiatan ?></a></td>
                    <td><?php echo $udesa['indikator_program']?></td>
                    <td><?php echo $udesa['indikator_keluaran']?></td>
                    <td style="text-align:center"><?php echo $udesa['nama_skpd']?></td>
                    <td style="text-align:center"><?php echo $udesa['tahun']?></td>
                    <td style="text-align:center;">
                        <a href="#collapseTwo" data-toggle="collapse" data-parent="#accordion2" class="btn btn-info btn-small bt-edit-udesa" name="<?php echo $udesa['id_usulankec']; ?>"><i class="icon-edit icon-white"></i>Ubah</a>
                        <a class="btn btn-danger btn-small bt-delete-udesa" name="<?php echo $udesa['id_usulankec']; ?>"><i class="icon-trash icon-white"></i>Hapus</a>
                        </td>
                </tr>
            <?php
}
}
?>